﻿#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endregion

namespace GameBase.Graphics
{
    /// <summary>
    /// Implementation of a helper component which allows full-screen rendering of scenes.
    /// </summary>
    public class QuadRenderer
    {
        #region Fields

        // list of vertices and its vertices which form the quad
        VertexPositionTexture[] verts = null;
        short[] ib = null;

        // obtain from the game the graphics device used to render the scene
        GraphicsDevice device;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="device">The GraphicsDevice that this QuadRenderer is associated to.</param>
        public QuadRenderer(GraphicsDevice device)
        {
            this.device = device;
            InitializeQuadRenderer();
        }

        /// <summary>
        /// Initializes the vertices of the QuadRenderer.
        /// </summary>
        private void InitializeQuadRenderer()
        {
            verts = new VertexPositionTexture[]
            {
                // 0 -> bottom right corner (1, -1)
                new VertexPositionTexture(Vector3.Zero, new Vector2(1, 1)),
                // 1 -> bottom left corner (-1, -1)
                new VertexPositionTexture(Vector3.Zero, new Vector2(0, 1)),
                // 2 -> top left corner (-1, 1)
                new VertexPositionTexture(Vector3.Zero, new Vector2(0, 0)),
                // 3 -> top right corner (1, 1)
                new VertexPositionTexture(Vector3.Zero, new Vector2(1, 0))
            };

            // define the position of the vertices
            Vector2 v1 = Vector2.One * -1;      // (-1, -1)
            Vector2 v2 = Vector2.One;           // (1, 1)
            // (1, -1)
            verts[0].Position.X = v2.X;
            verts[0].Position.Y = v1.Y;
            // (-1, -1)
            verts[1].Position.X = v1.X;
            verts[1].Position.Y = v1.Y;
            // (-1, 1)
            verts[2].Position.X = v1.X;
            verts[2].Position.Y = v2.Y;
            // (1, 1)
            verts[3].Position.X = v2.X;
            verts[3].Position.Y = v2.Y;

            // traverse the triangles that define the quad clockwise
            // { 0, 1, 2 } -> bottom triangle
            // { 2, 3, 0 } -> top triangle
            ib = new short[] { 0, 1, 2, 2, 3, 0 };
        }

        #endregion

        #region Draw

        /// <summary>
        /// Draws the scene using the QuadRenderer.
        /// </summary>
        public void Draw()
        {
            device.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, verts, 0, 4, ib, 0, 2);
        }

        #endregion
    }
}
